<template>
	<el-container>
		<el-aside width="300px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="id" :data="group" :current-node-key="''" :highlight-current="true"
							 :props="groupProps" default-expand-all
							 :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" plain size="small" icon="el-icon-refresh" @click="groupRefresh()"></el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container>
				<el-header>
					<div class="left-panel">
						<el-button type="primary" plain icon="el-icon-refresh" @click="tableRefresh"></el-button>
						<el-button type="primary" icon="el-icon-plus" @click="add" v-if="permissions.add"></el-button>
						<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length===0"
								   @click="batch_del" v-if="permissions.delete"></el-button>
					</div>
					<div class="right-panel">
						<div class="right-panel-search">
							<el-input v-model="search.keyWord" placeholder="关键词" clearable></el-input>
							<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						</div>
					</div>
				</el-header>
				<el-main class="nopadding table-no-permission" v-if="!permissions.search">
					没有列表查看权限
				</el-main>
				<el-main class="nopadding" v-else>
					<scTableGdshop ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter>
						<el-table-column type="selection" width="50" align="center"></el-table-column>
						<el-table-column label="标题" prop="title" align="center">
							<template #header>
								<div>标题</div>
							</template>
							<template #default="scope">
								<div>ID:{{ scope.row.id }}</div>
								<div>{{ scope.row.title }}</div>
							</template>
						</el-table-column>
						<el-table-column label="分类" prop="cat_name" align="center"></el-table-column>
						<el-table-column label="封面" prop="cover" align="center" width="110">
							<template #default="scope">
								<el-image style="width: 80px; height: 80px" :src="showImage(scope.row.cover)"
										  :preview-src-list="[showImage(scope.row.cover)]" />
							</template>
						</el-table-column>
						<el-table-column label="排序" prop="sort" align="center"></el-table-column>
						<el-table-column label="创建时间" prop="create_at" width="150" align="center">
							<template #header>
								<div>创建时间</div>
								<div>修改时间</div>
							</template>
							<template #default="scope">
								<div>{{ showDateTime(scope.row.create_at) }}</div>
								<div>{{ showDateTime(scope.row.update_at) }}</div>
							</template>
						</el-table-column>
						<el-table-column label="操作" fixed="right" align="right" width="140">
							<template #default="scope">
								<el-button v-if="permissions.edit" type="text" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
								<template v-if="permissions.delete">
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button type="text" size="small">删除</el-button>
									</template>
								</el-popconfirm>
								</template>
							</template>
						</el-table-column>

					</scTableGdshop>
				</el-main>
		</el-container>
	</el-container>

	<edit-dialog v-if="dialog.save" ref="editDialog" @success="handleSuccess" @closed="dialog.save=false"></edit-dialog>

</template>

<script>
import editDialog from './edit'
import { permission } from '@/utils/permission'

export default {
		name: 'user',
		components: {
			editDialog
		},
		data() {
			return {
				dialog: {
					save: false
				},
				showGrouploading: false,
				groupFilterText: '',
				group: [],
				groupProps: {
					label: 'cat_name',
					value: 'id'
				},
				apiObj: this.$API.cms.links,
				selection: [],
				search: {
					keyWord: ''
				},
				baseUrl: '',
				categoryType: 4,
				permissions: { // 权限，统一处理
					add: permission('cms:links:add'),
					edit: permission('cms:links:edit'),
					delete: permission('cms:links:delete'),
					search: permission('cms:links:search')
				}
			}
		},
		watch: {
			groupFilterText(val) {
				this.$refs.group.filter(val);
			}
		},
		mounted() {
			this.getGroup()
			this.getBaseUrl()
		},
		methods: {
			showDateTime(_time) {
				if (!_time) {
					return "-";
				}
				return this.$TOOL.dateFormat(_time * 1000);
			},
			async getBaseUrl() {
				this.baseUrl = await this.$TOOL.getBaseUrl(this.$API)
			},
			showImage(url) {
				return this.baseUrl + url;
			},
			tableRefresh() {
				this.$refs.table.refresh();
			},
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.editDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.editDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.editDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row, index){
				var reqData = {ids: [row.id]}
				var res = await this.apiObj.delete(reqData);
				if(res.code === 0){
					//这里选择刷新整个表格 OR 插入/编辑现有表格数据
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(async () => {
					const loading = this.$loading();
					var reqData = {ids: this.selection.map(e => e.id)}
					var res = await this.apiObj.delete(reqData);
					loading.close();
					if (res.code === 0) {
						this.$message.success("操作成功")
						this.tableRefresh()
					}
					else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//加载树数据
			async getGroup(){
				this.showGrouploading = true;
				var res = await this.$API.cms.category.list({type:this.categoryType})
				this.showGrouploading = false;
				var allNode ={id: 0, cat_name: '所有'}
				res.data.unshift(allNode);
				this.group = this.$TOOL.deepTree(res.data, 'parent_id');
			},
			//树过滤
			groupFilterNode(value, data){
				if (!value) return true;
				return data.cat_name.indexOf(value) !== -1;
			},
			//树点击事件
			groupClick(data){
				var params = {}
				if (data.id > 0) {
					let tmp = this.$TOOL.revDeepTree([data],'parent_id')
					params.categoryIds = tmp.map(e => e.id)
				}
				this.$refs.table.reload(params);
			},
			groupRefresh() {
				this.getGroup()
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode){
				if(mode==='add'){
					this.$refs.table.reload();
				}else if(mode==='edit'){
					this.$refs.table.tableData.filter(item => item.id===data.id ).forEach(item => {
						Object.assign(item, data)
					})
				}
			}
		}
	}
</script>

<style>
</style>
